package com.nidu.demo.menu.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.menu.convertor.MenuConvertor;
import com.nidu.demo.menu.convertor.MenuCriteriaConvertor;
import com.nidu.demo.menu.dto.MenuPageQry;
import com.nidu.demo.menu.dto.MenuTreeCO;
import com.nidu.demo.menu.model.MenuCriteria;
import com.nidu.demo.menu.util.MenuUtil;
import com.nidu.demo.menu.gateway.MenuGateway;
import com.nidu.demo.menu.model.Menu;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 菜单分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MenuPageQryExe {

    private final MenuGateway gateway;

    private final MenuConvertor convertor;

    private final MenuCriteriaConvertor criteriaConvertor;

    public PageResponse<MenuTreeCO> execute(MenuPageQry qry) {
        MenuCriteria criteria = criteriaConvertor.toDataObject(qry);
        PageResponse<Menu> page = gateway.pageByCondition(criteria);
        List<MenuTreeCO> menuTreeCOS = convertor.toTreeClientObjects(page.getData());
        List<MenuTreeCO> treeCOS = MenuUtil.buildMenuTree(menuTreeCOS);
        return PageResponse.of(treeCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }
}